$(document).ready(function() {
  // $('.p-remain').html(window.location.search);
  // 微信登录
  if (isWechat() && wxUserInfo.nickname != '') {
    wxLogin(wxUserInfo, (function(result) {
      userData = getUserInfo(result);
      callbackAfterLogin(userData);
    }), ifBind);
  }
  //app登录
 // if (getUrlParam('DEVICE') == 'ios' || getUrlParam('DEVICE') == 'android') {
 //    var _data = {};
 //    _data.id = getUrlParam('USER_ID') || '';
 //    _data.token = getUrlParam('TOKEN') || '';
 //
 //    if (_data.id == '') {
 //      console.log('app未登录');
 //      window.location.href = 'lalocal://app?{"errorCode": "401"}';
 //    }
 //    else {
 //      userData = getUserInfo(_data);
 //      callbackAfterLogin(userData);
 //    }
 //  }
  if (getUrlParam('DEVICE') == 'ios' || getUrlParam('DEVICE') == 'android') {
    isApp = true;
   }

});

/**
 * 弹出“是否绑定邮箱”弹窗
 * ifBind
 */
function ifBind() {
  $('.box-if-bind').show();
  $('.btn-bind-yes').on('click', function() {
    $('.box-if-bind').hide();
    showEmailForm();
  });
  $('.btn-bind-no').on('click', function() {
    $('.box-if-bind').hide();
    console.log('你选择了不绑定,接下来三方注册。');
    //------三方注册--------
    wxRegister(wxUserInfo, (function(result) {
      userData = getUserInfo(result);
      callbackAfterLogin(userData);

    }), function() {
      console.log('微信注册失败！')
    });
  });
}
/**
 * 显示绑定邮箱表单
 * showEmailForm
 */
function showEmailForm() {
  $('.box-email-form').show();
  $('.btn-email-ok').on('click', function() {
    var emailInfo = {};
    emailInfo.email = $('input[name="email"]').val();
    emailInfo.password = $.md5($('input[name="password"]').val());
    checkEmail(wxUserInfo, emailInfo, function(result) {
        $('.box-email-form').hide();
        alert('邮箱绑定成功！')
        userData = getUserInfo(result);
        callbackAfterLogin(userData);

      },
      function() {
        alert('邮箱或者密码输入有误。');
      })
  });
  $('.btn-email-cancel').on('click', function() {
    $('.box-email-form').hide();
    console.log('你选择了取消绑定，接下来三方注册。');
    // ------三方注册--------
    wxRegister(wxUserInfo, (function(result) {
      userData = getUserInfo(result);
      callbackAfterLogin(userData);

    }), function() {
      console.log('微信注册失败！')
    });
  });
}

/**
 * 提取需要的用户数据，返回一个对象
 * @param  {object} result 未封装的用户信息（登录后）
 * @return {object} obj 封装的用户数据
 */

function getUserInfo(result) {
  var _data = {};
  _data.id = result.id;
  _data.token = result.token;
  return _data;
};

function callbackAfterLogin(userData) {
  console.log('callbackAfterLogin');
  checkActive(userData, '1', function(data) {
    $('.p-remain').html('限时 ' + data.result.amount.toString() + ' 份');
    if (data.result.amount == 0) {
      isZero = true;
    }
    isAvailable = data.result.available;
  }, function(data) {});
  checkActive(userData, '2', function(data) {
    isAvailable2 = data.result.available;
  }, function(data) {});
}

$('.img-btn-get').on('click', function() {
  if(isApp) {
    $('.pop-share-app').show();
  }
  else if (isZero) {
    alert('今天的乐钻已经被领光啦,明天早点来抢哦！');
  }
  else if (isAvailable) {
    joinActive(userData, '1', function(data) {
      checkActive(userData, '1', function(data) {
        $('.p-remain').html('限时 ' + data.result.amount.toString() + ' 份');
        isAvailable = data.result.available;
      }, function(data) {});
      $('.box-grey').show();
      $('.pop-2').show();
    }, function(data) {
      alert('领取失败，请刷新重试！')
    });
  }
  else {
    $('.box-grey').show();
    $('.pop-1').show();
  }
});


$('.img-btn-route').on('click', function() {
  window.location.href = 'routes';
});

$('.img-cancel').on('click', function() {
  $('.box-grey').hide();
  $('.pop').hide();
});
$('.pop-share').on('click', function() {
  $('.pop-share').hide();
  $('.box-grey').hide();
  $('.pop').hide();
});
$('.pop-share-app').on('click', function() {
  $('.pop-share-app').hide();
});
$('.share-btn').on('click', function() {
  $('.pop-share').show();
});

$('.download').on('click', function() {
  if(isWechat()){
    window.location.href = 'http://a.app.qq.com/o/simple.jsp?pkgname=com.lalocal.lalocal';
  }
});
document.body.addEventListener('touchmove', function(event) {
  event.preventDefault();
}, false);
